#include <opencv2\opencv.hpp>
#include <qdebug.h>
#include "../include/saver.h"

void disk::water(std::string file_path)
{
	cv::FileStorage Writer(file_path, cv::FileStorage::WRITE);
	/*if (Writer.isOpened())
	{
		qDebug() << "opened " << file_path.c_str();
	}
	else
	{
		qDebug() << "cannot opened" << file_path.c_str();
	}
	Writer << "UserLables" << "[";
	for (int i = 0; i < 45; i++)
	{
		Writer << (std::string("user") + std::to_string(i));
	}
	Writer << "]";
	Writer << "water" << 1;*/
	Writer.release();
	/*cv::FileStorage Reader(file_path, cv::FileStorage::READ);
	qDebug() << "water = " << (int)Reader["water__"];
	Reader.release();*/
}

void disk::getParamYaml(UserConfig* config_ptr, std::string file_path)
{
	qDebug() << "read" << file_path.c_str();
	cv::FileStorage Reader(file_path, cv::FileStorage::READ);
	
	Reader["NodeTotalNum"] >> config_ptr->NodeTotalNum;
	Reader["AnnuRows"] >> config_ptr->AnnuRows;
	Reader["AnnuCols"] >> config_ptr->AnnuCols;

	Reader["UpdateStamp"]["Year"] >> config_ptr->update_stamp.Year;
	Reader["UpdateStamp"]["Month"] >> config_ptr->update_stamp.Month;
	Reader["UpdateStamp"]["Day"] >> config_ptr->update_stamp.Day;
	Reader["UpdateStamp"]["Hour"] >> config_ptr->update_stamp.Hour;
	Reader["UpdateStamp"]["Minute"] >> config_ptr->update_stamp.Minute;
	Reader["UpdateStamp"]["Second"] >> config_ptr->update_stamp.Second;

	Reader["DateRange"]["BeginDate"]["Year"] >> config_ptr->begin_date.Year;
	Reader["DateRange"]["BeginDate"]["Month"] >> config_ptr->begin_date.Month;
	Reader["DateRange"]["BeginDate"]["Day"] >> config_ptr->begin_date.Day;
	Reader["DateRange"]["EndDate"]["Year"] >> config_ptr->end_date.Year;
	Reader["DateRange"]["EndDate"]["Month"] >> config_ptr->end_date.Month;
	Reader["DateRange"]["EndDate"]["Day"] >> config_ptr->end_date.Day;

	cv::FileNode lableNode = Reader["UserLables"];
	cv::FileNodeIterator it = lableNode.begin(), it_end = lableNode.end();
	for (int i = 0; i < config_ptr->NodeTotalNum; i++)
	{
		if (it == it_end)
			break;
		(*it) >> config_ptr->user_lables[i];
		it++;
	}
	Reader.release();
}

void disk::saveParamYaml(UserConfig* config_ptr, std::string file_path)
{
	qDebug() << "write" << file_path.c_str();
	cv::FileStorage Writer(file_path, cv::FileStorage::WRITE);

	Writer << "NodeTotalNum" << config_ptr->NodeTotalNum;

	Writer << "DateRange" << "{"
		<< "BeginDate" << "{" 
		<< "Year" << config_ptr->begin_date.Year
		<< "Month" << config_ptr->begin_date.Month
		<< "Day" << config_ptr->begin_date.Day
		<< "}"
		<< "EndDate" << "{"
		<< "Year" << config_ptr->end_date.Year
		<< "Month" << config_ptr->end_date.Month
		<< "Day" << config_ptr->end_date.Day
		<< "}"
		<< "}";

	Writer << "UpdateStamp" << "{"
		<< "Year" << config_ptr->update_stamp.Year
		<< "Month" << config_ptr->update_stamp.Month
		<< "Day" << config_ptr->update_stamp.Day
		<< "Hour" << config_ptr->update_stamp.Hour
		<< "Minute" << config_ptr->update_stamp.Minute
		<< "Second" << config_ptr->update_stamp.Second
		<< "}";

	Writer << "AnnuRows" << config_ptr->AnnuRows;
	Writer << "AnnuCols" << config_ptr->AnnuCols;

	Writer << "UserLables" << "[";
	for (int i = 0; i < config_ptr->NodeTotalNum; i++)
	{
		Writer << config_ptr->user_lables[i];
	}
	Writer << "]";

	Writer.release();
}

void disk::saveMonthYaml(MonthData& month_data, std::string file_path)
{
	qDebug() << "write" << file_path.c_str();
	cv::FileStorage Writer(file_path, cv::FileStorage::WRITE);
	Writer << "DayDataList" << "[";
	MonthData::iterator day_iter = month_data.begin();
	for (; day_iter != month_data.end(); day_iter++)
	{
		Writer << "{";
		Writer << "DayIndex" << day_iter->first;
		Writer << "MomentList" << "[";
		DayData::iterator moment_iter = day_iter->second.begin();
		for (; moment_iter != day_iter->second.end(); moment_iter++)
		{
			Writer << "{";
			Writer << "Hour" << moment_iter->hour;
			Writer << "Minter" << moment_iter->minute;
			Writer << "PowerON" << moment_iter->PowerON;
			Writer << "}";
		}
		Writer << "]";
		Writer << "}";
	}
	Writer << "]";
	Writer.release();
}

void disk::getMonthYaml(MonthData& month_data, std::string file_path)
{
	qDebug() << "read" << file_path.c_str();
	month_data.clear();
	cv::FileStorage Reader(file_path, cv::FileStorage::READ);
	cv::FileNode DayDataNode = Reader["DayDataList"];
	cv::FileNodeIterator it_day = Reader["DayDataList"].begin();
	for (; it_day != DayDataNode.end(); it_day++)
	{
		int day_index = 0;
		(*it_day)["DayIndex"] >> day_index;
		cv::FileNodeIterator it_moment = (*it_day)["MomentList"].begin();
		for (; it_moment != (*it_day)["MomentList"].end(); it_moment++)
		{
			HourMin temp_moment;
			(*it_moment)["Hour"] >> temp_moment.hour;
			(*it_moment)["Minter"] >> temp_moment.minute;
			(*it_moment)["PowerON"] >> temp_moment.PowerON;
			month_data[day_index].push_back(temp_moment);
		}
	}
	Reader.release();
}

void disk::delectYaml(std::string file_path)
{
	qDebug() << "delect" << file_path.c_str();
	cv::FileStorage Writer(file_path, cv::FileStorage::WRITE);
	Writer.release();
}